package com.glorysoft.tonglingxjpda.utils

import android.content.Context
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Environment
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers
import java.io.File

/**
 * FileName: FileUtils
 * Author: XJH
 * Date: 2021/5/20 17:30
 * Description:
 * History:
 */
class FileUtils {
    companion object {
         //判断文件是否存在
        public fun fileIsExists(strFile: String?): Boolean {
            try {
                val f = File(strFile)
                if (!f.exists()) {
                    return false
                }
            } catch (e: java.lang.Exception) {
                return false
            }
            return true
        }

        fun isAndroidQ(): Boolean {
            return Build.VERSION.SDK_INT >= 29
        }

        /**
         * @return
         */
        fun getDownloadDataPath(context: Context): String {
            return if (isAndroidQ()) {
                context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                    .toString() + File.separator + "TLXJDATAS" + File.separator
            } else {
                FreeApi_StaticMembers.ROOT_PATH + "/TLXJDATAS/"
            }
        }


        /**
         * 获取本地文件下载地址
         *
         * @return
         */
        fun getLocalPathAndFileName(context: Context): String {
            return getDownloadDataPath(context)
        }


        /**
         * 判断文件是否是图片
         *
         * @param path
         * @return
         */
        fun checkImage(path: String): Boolean {
            return if (path.endsWith("jpg") || path.endsWith("png") || path.endsWith("jpeg")) {
                true
            } else BitmapFactory.decodeFile(path) != null
        }


    }


}